package com.aixingfu.sports.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aixingfu.sports.MVP.contract.ContractSignSaveContract;
import com.aixingfu.sports.MVP.presenter.ContractSignSavePresenter;
import com.aixingfu.sports.Model.ContractSignSaveModel;
import com.aixingfu.sports.R;
import com.aixingfu.sports.bean.AutographPage;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.orie.librarybase.base.BaseActivity;
import com.vondear.rxtool.view.RxToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//电子签名
@Route(path = "/main/AutographActivity")
public class AutographActivity extends BaseActivity implements ContractSignSaveContract.MyView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_sign_customer)
    RelativeLayout rlSignCustomer;
    @BindView(R.id.rl_membership_consultant)
    RelativeLayout rlMembershipConsultant;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.iv_customer_signature)
    ImageView ivCustomerSignature;
    @BindView(R.id.iv_membership_consultant)
    ImageView ivMembershipConsultant;
    @BindView(R.id.ll_customer)
    LinearLayout llCustomer;
    @BindView(R.id.ll_membership)
    LinearLayout llMembership;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private String order_id;
    private String member_sign="";
    private String huiji_sign="";
    private String contract_id = "";
    private String renewal_way = "";
    private ContractSignSavePresenter contractSignSavePresenter;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_autograph;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/AutographActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        Bundle bundle = getIntent().getExtras();
        order_id = bundle.getString("order_id", "");
        contract_id = bundle.getString("contract_id", "");
        renewal_way = bundle.getString("renewal_way", "");
        contractSignSavePresenter = new ContractSignSavePresenter();
        contractSignSavePresenter.attachView(this);
    }

    @Override
    protected void initEvent() {

    }
    public void getChargeContractSignSave(){
        if (contractSignSavePresenter.isViewAttached()){
            HashMap<String,String> map = new HashMap<>();
            map.put("contract_id",contract_id);
            map.put("member_sign",member_sign);
            map.put("huiji_sign",huiji_sign);
            contractSignSavePresenter.getContractSignSave(map);
        }
    }
    @Override
    protected void beforeInit() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AutographPage event) {
        String type = event.getType();
        if ("1".equals(type)) {
            llCustomer.setVisibility(View.GONE);
            Glide.with(AutographActivity.this).load(event.getData()).into(ivCustomerSignature);
            member_sign = event.getData();
        } else if ("2".equals(type)) {
            llMembership.setVisibility(View.GONE);
            Glide.with(AutographActivity.this).load(event.getData()).into(ivMembershipConsultant);
            huiji_sign = event.getData();
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back,R.id.rl_sign_customer, R.id.rl_membership_consultant, R.id.tv_back, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_sign_customer:
//                AutographPageActivity.startActivityByRoute();
                ARouter.getInstance().build("/main/AutographPageActivity").withString("type", "1").navigation();
                break;
            case R.id.rl_membership_consultant:
                ARouter.getInstance().build("/main/AutographPageActivity").withString("type", "2").navigation();
//                AutographPageActivity.startActivityByRoute();
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_submit:
                if (TextUtils.isEmpty(member_sign) || TextUtils.isEmpty(huiji_sign)){
                    RxToast.normal(AutographActivity.this, "请上传签名").show();
                }else {
//                    getChargeContractSignSave();
                    ARouter.getInstance().build("/main/ContractDisplayActivity").withString("order_id",order_id).navigation();
                }
//                ARouter.getInstance().build("/main/ContractDisplayActivity").withString("order_id",order_id).navigation();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销订阅者
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showToast(String msg, String state) {

    }

    @Override
    public void setContractSignSave(ContractSignSaveModel data) {

    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void hideLoading() {
        dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return null;
    }
}
